﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Client.modules.output
{
    /*
     * 用于浏览器下载文件的类
     */
    public partial class httpProxy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DownLoadFile();          
        }
        bool DownLoadFile()
        {
            bool check = false;
            string url = Server.UrlDecode(Server.UrlDecode(Request.Params["fileURL"]));
            if (!string.IsNullOrEmpty(url) && (url.Contains("http://") || url.Contains("https://") || url.StartsWith("../")))
            {
                string filePath = Server.UrlDecode(Server.UrlDecode(Request.Params["filePath"]));           
                string fileName = (Request.Params["fileName"] == null) ? filePath.Substring(filePath.LastIndexOf("\\") + 1) : Server.UrlDecode(Server.UrlDecode(Request.Params["fileName"]));
              try  {
                    Response.Clear();
                    Response.ClearHeaders();
                    FileInfo f = new FileInfo(filePath);
                    Response.ContentType = GetFileContentType(f.Extension);
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlPathEncode(fileName));
                    Response.AddHeader("Content-Length", f.Length.ToString());
                    Response.TransmitFile(filePath);
                    check = true;
                }
                catch (Exception ex)
                {
                    check = false;
                }
            }
            return check;
        }
        string GetFileContentType(string fileExt)
        {
            string type = "application/octet-stream";
            switch (fileExt)
            {
                case ".tif":
                case ".tiff":
                    type = "image/tiff";
                    break;
                case ".ppt":
                    type = "application/vnd.ms-powerpoint";
                    break;
                case ".pptx":
                    type = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                    break;
                case ".pdf":
                    type = "application/pdf";
                    break;
                case ".doc":
                    type = "application/msword";
                    break;
                case ".docx":
                    type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    break;
                case ".dwg":
                    type = "application/x-dwg";
                    break;
                case ".png":
                    type = "image/png";
                    break;
                case ".bmp":
                    type = "image/bmp";
                    break;
                case ".gif":
                    type = "image/gif";
                    break;
                case ".xls":
                    type = "application/vnd.ms-excel";
                    break;
                case ".xlsx":
                    type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    break;
                case ".jpeg":
                    type = "image/jpeg";
                    break;
            }
            return type;
        }
    }
}